package com.kikyou.api;


import com.kikyou.pojo.BloodType;
import com.kikyou.pojo.Cities;
import com.kikyou.pojo.Nation;
import com.kikyou.pojo.Province;
import com.kikyou.service.impl.BloodTypeServiceImpl;
import com.kikyou.service.impl.CitiesServiceImpl;
import com.kikyou.service.impl.NationServiceImpl;
import com.kikyou.service.impl.ProvinceServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Properties;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ltm
 * @since 2022-03-07
 */
@RestController
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private NationServiceImpl nationService;
    @Autowired
    private  BloodTypeServiceImpl bloodTypeService;
    @Autowired
    private ProvinceServiceImpl provinceServiceImpl;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CitiesServiceImpl citiesServiceImpl;

    @ApiOperation(value = "查询民族列表")
    @GetMapping("/nation")
    public List<Nation> findAllNation(){
        return nationService.findAllNation();
    }

    @ApiOperation(value = "查询血型列表")
    @GetMapping("/blood")
    public List<BloodType> findAllBloodType(){
        return bloodTypeService.findAllBloodType();
    }
    @ApiOperation(value = "查询省")
    @GetMapping("/province")
    public List<Province> findAllProviders(){
        //查询redis是否有省数据，没有则从数据库查询
        ListOperations listOperations = redisTemplate.opsForList();
        List<Province> provinces =  listOperations.range("province", 0, -1);
        if(CollectionUtils.isEmpty(provinces)){
            provinces =provinceServiceImpl.list();
            listOperations.rightPushAll("provinces",provinces);
            return  provinces;
        }
        return provinces;
    }

    @ApiOperation(value = "查询热门城市")
    @GetMapping("/hotCities")
    public List<Cities> findHotCites() {
        return citiesServiceImpl.findHotCites();
    }
}
